/**
 * Copyright (c) 2020 NumberFour AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   NumberFour AG - Initial API and implementation
 */
package org.eclipse.n4js.transpiler.es.tests;

import static org.eclipse.n4js.tests.helper.mock.MockWorkspaceSupplier.TEST_PROJECT__NAME;

import org.eclipse.n4js.N4JSInjectorProvider;
import org.eclipse.n4js.n4JS.Script;
import org.eclipse.xtext.testing.InjectWith;
import org.eclipse.xtext.testing.XtextRunner;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(XtextRunner.class)
@InjectWith(N4JSInjectorProvider.class)
public class SimplifyTransformationTest extends AbstractTranspilerTest {

	@Test
	public void testSimplifyPropertyNames() throws Exception {
		Script script = testHelper.parseAndValidateSuccessfully("""
				@StringBased
				enum E {
					LitIdentifier: "enumLiteralIdentifier",
					LitNotAnIdentifier: "enumLiteral Not An Identifier"
				}

				// cases that should be fully simplified
				({
					"prop1": 42,
					["prop2"]: 42,
					["prop" + 3]: 42,
					[E.LitIdentifier]: 42,
				});

				// cases that should be partially simplified
				({
					["not an identifier1"]: 42,
					["not an identifier" + 2]: 42,
					[E.LitNotAnIdentifier]: 42,
				});

				// cases that must not be simplified
				({
					"not an identifier3": 42,
					[21*2]: 42,
					[Symbol.iterator]: 42
				});

				class Fully {
					"method1"() {}
					["method2"]() {}
					["method" + 3]() {}
					[E.LitIdentifier]() {}
				}
				class Partially {
					["not an identifier1"]() {}
					["not an identifier" + 2]() {}
					[E.LitNotAnIdentifier]() {}
				}
				class NotAtAll {
					"not an identifier3"() {}
					[21*2]() {}
					[Symbol.iterator]() {}
				}
				""");

		assertCompileResult(script,
				"""
						// Generated by N4JS transpiler; for copyright see original N4JS source file.

						import 'n4js-runtime'

						({
							prop1: 42,
							prop2: 42,
							prop3: 42,
							enumLiteralIdentifier: 42
						});
						({
							'not an identifier1': 42,
							'not an identifier2': 42,
							'enumLiteral Not An Identifier': 42
						});
						({
							'not an identifier3': 42,
							[21 * 2]: 42,
							[Symbol.iterator]: 42
						});
						class Fully extends N4Object {
							method1() {}
							method2() {}
							method3() {}
							enumLiteralIdentifier() {}
							static get n4type() {
								return $getReflectionForClass(this, '["Fully","__synthetic0","%s"]');
							}
						}
						class Partially extends N4Object {
							'not an identifier1'() {}
							'not an identifier2'() {}
							'enumLiteral Not An Identifier'() {}
							static get n4type() {
								return $getReflectionForClass(this, '["Partially","__synthetic0","%s"]');
							}
						}
						class NotAtAll extends N4Object {
							'not an identifier3'() {}
							[21 * 2]() {}
							[Symbol.iterator]() {}
							static get n4type() {
								return $getReflectionForClass(this, '["NotAtAll","__synthetic0","%s",["m.#iterator"]]');
							}
						}
						""".formatted(TEST_PROJECT__NAME, TEST_PROJECT__NAME, TEST_PROJECT__NAME));
	}
}
